<script setup>
import {ref, computed, onMounted} from 'vue'
// const props = defineProps({})
const useRate = () => {
  const current = ref(0); // 风险等级
  const widthRef = ref(0);  //
  const scoreRef = ref(30) // 得分
  const leftStyle = computed(() => {
    return {
      left: widthRef.value + '%'
    }
  })
  const widthStyle = computed(() => {
    return {
      width: widthRef.value + '%'
    }
  })
  const setClick = (index) => {
    current.value = index;
    const rnum = Math.random() * 100;
    scoreRef.value = widthRef.value = Math.floor(rnum);
  }
  onMounted(() => {
    setTimeout(() => {
      widthRef.value = Math.random() * 100;
    }, 500)
  })
  return {
    current, widthRef, scoreRef, leftStyle, widthStyle, setClick
  }

}
const {current, scoreRef, leftStyle, widthStyle, setClick} = useRate()
</script>

<template>
  <div class="risk">
    <van-row gutter="6">
      <van-col span="6">
        <div class="risk-wrap" @click="setClick(0)" :class="{'active':current === 0}">
          <div class="risk__score" :style="leftStyle">
            <span>{{ scoreRef }}分</span>
          </div>
          <div class="risk__item">
            <div class="process" :style="widthStyle"></div>
          </div>
          <div class="risk__txt">高风险</div>
        </div>
      </van-col>
      <van-col span="12">
        <div class="risk-wrap" @click="setClick(1)" :class="{'active':current === 1}">
          <div class="risk__score" :style="leftStyle">
            <span>{{ scoreRef }}分</span>
          </div>
          <div class="risk__item">
            <div class="process" :style="widthStyle"></div>
          </div>
          <div class="risk__txt">中风险</div>
        </div>
      </van-col>
      <van-col span="6">
        <div class="risk-wrap" @click="setClick(2)" :class="{'active':current === 2}">
          <div class="risk__score" :style="leftStyle">
            <span>{{ scoreRef }}分</span>
          </div>
          <div class="risk__item">
            <div class="process" :style="widthStyle"></div>
          </div>
          <div class="risk__txt">低风险</div>
        </div>
      </van-col>
    </van-row>
  </div>
</template>
<style scoped lang="scss">
$mainColor: #FF626D;
$colobBg: #FFECED;
.risk-wrap {
  display: flex;
  flex-direction: column;

  &.active {
    .risk__score,
    .risk__item .process {
      visibility: visible;
      transition: all 0.5s;
    }

    .risk__txt {
      color: $mainColor;
    }
  }

  .risk__score {
    visibility: hidden;
    position: relative;
    left: 50%;
    transform: translate(-50%, 0);
    flex: none;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 16px;
    width: 50px;
    height: 22px;
    background: $colobBg;
    border: 1px solid $mainColor;

    span {
      text-align: center;
      color: $mainColor;
      font-family: PingFangSC-Regular;
      font-weight: 400;
      font-size: 12px;
      line-height: 17px;
    }

    &::after {
      position: absolute;
      left: 50%;
      top: 20px;
      content: " ";
      height: 6px;
      border-left: 1px solid $mainColor;
      margin-left: -1px;
    }
  }

  .risk__item {
    margin-top: 5px;
    width: 100%;
    height: 6px;
    background: #F1F1F3;
    overflow: hidden;

    .process {
      //transition:all 0.5s;
      visibility: hidden;
      width: 2%;
      height: 6px;
      background: $mainColor;
    }
  }

  .risk__txt {
    margin-top: 4px;
    text-align: center;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    font-size: 12px;
    line-height: 17px;
  }
}

</style>